package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import com.model.Cal;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/cal")
public class CalServlet extends HttpServlet {

	@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			this.doPost(req, resp);
	}

	@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			//设置编码格式,防止乱码
			req.setCharacterEncoding("utf-8");
			resp.setContentType("text/html;charset=utf-8");
			//第1步: 获取参数
			String num1 = req.getParameter("num1");
			String num2 = req.getParameter("num2");
			String type = req.getParameter("type");
			//第2步: 实例化一个对象
			Cal cal = new Cal();
			//第3步: 给这个对象赋值
			cal.setA(Double.parseDouble(num1));
			cal.setB(Double.parseDouble(num2));
			cal.setC(type);
			//第4步: 调用对象的getResult方法,将结果保存到result里面
			double result = cal.getResult();
			//打印结果
			PrintWriter out = resp.getWriter();
			out.println("结果为:"+result);	
	}	
}
